<?php
/*
 * 商家用户类
 */
class User_Merchant extends User_Base 
{
	private $_merchant_info = NULL;
	private $_shop_id = NULL;

	/*
	 * 商家联系方式
	 * @return array(
	 *		'wangwang' => array($w1, $w2),
	 *		'qq'       => array(...),
	 *		'msn'      => array(...),
	 *	);
	 */
	public static function contact_ways ($shop_id)
	{
		$shop_id = intval($shop_id);
		$contact = Model_Shopinfo::list_shopkeeper_online($shop_id);
		return $contact;
	}


	public function __construct($uid = NULL, $user_info = NULL)
	{
		$this->_role_id   = 'merchant';
		$this->_user_id   = $uid;
		$this->_user_info = $user_info;

        $this->_user_info['role_id'] = $this->_role_id;

		parent::__construct($uid);
	}

    public function has_joined_CPS ()
    {
        $acts     = Promotion::activities_by_merchant(user::current_user_id());
        foreach ($acts as $act)
        {
            $pay_type = Arr::get($act, 'pay_type', 'CPA');
            if ($pay_type == 'CPS')
            {
                return TRUE;
            }
        }

        return FALSE;
    }

	public function get_alipay_account ()
	{
		$shop = shop_info::merchant_shops($this->_user_id);
		if (empty($shop))
			return 'wentianjiang@sohu.com';
		else
			return $shop[0]['alipay_account'];
	}


	/*
	 * @return array ($shop_id1, $shop_id2....)
	 */
	public function shops ()
	{
	}

	public function shop_id ()
	{
		if (NULL === $this->_shop_id)
		{
			$shops = Shop_Info::merchant_shops ($this->_user_id);
			if (empty($shops))
				$this->_shop_id = 0;
			else if (count($shops) > 0 && isset($shops[0]['id']))
				$this->_shop_id = $shops[0]['id'];
			else
				$this->_shop_id = 0;
		}

		return $this->_shop_id;
	}

    public function merchant_info ()
    {
        $cache = Cache::instance('memcache');
        $key   = self::merchant_info_key($this->_user_id);
        $info  = $cache->get($key);

        if (empty($info))
        {
            $info = array(
                'activities' => array(),
                'types'      => array(),
            );
            $types = array();
            $acts = Promotion::activities_by_merchant($this->_user_id);
            foreach ($acts as $act)
            {
                $aid = $act['id'];
                $info['activities'][$aid] = $act;
                $types[] = $act['pay_type'];
            }

            $info['types'] = array_unique ($types);
            $cache->set($key, $info, 1800);
        }

        return $info;
    }

    private static function merchant_info_key ($uid)
    {
        return "merchant_info_$uid";
    }
}
